#include "openssl/ssl.h"
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <omp.h>

#define DEBUG 1
#define PRIVKEY "static unsigned char privkey[%d] = {"
#define PUBKEY "static const unsigned char pubkey[%d] = {"
#define ENDKEY "\n};\n"

void display_sig(ECDSA_SIG *sig){
	char      *tmp_char=NULL;
	
        tmp_char = OPENSSL_malloc(128);
        if (tmp_char == NULL) { fprintf(stderr, "Error during allocation of OpenSSL\n");}
        tmp_char = BN_bn2dec(sig->r);
        fprintf(stdout, "sig->r = %s\n", tmp_char);
        tmp_char = BN_bn2dec(sig->s);
        fprintf(stdout, "sig->s = %s\n", tmp_char);

	return;
}

int main(int argc, char * argv[]){
	int        i=0,len;
	ECDSA_SIG *sig;
	EC_KEY    *eckey = EC_KEY_new();
	EC_GROUP  *group = NULL;
	clock_t start, end;
	double cpu_time_used;


	SSL_load_error_strings();
	ERR_load_BIO_strings();
	OpenSSL_add_all_algorithms();

	
	if (eckey == NULL) { fprintf(stderr,"Error during the allocation of the EC_KEY object !\n"); }
	group = EC_GROUP_new_by_curve_name(NID_secp160k1);
	if (group == NULL) { fprintf(stderr,"Error during the EC_GROUP object\n"); }
	EC_KEY_set_group(eckey, group);
	if (!EC_KEY_generate_key(eckey)) {
		fprintf(stderr, "Error EC_KEY_generate_key\n");
		exit(-1);
	}
	
	start = clock();
#pragma parallele for
	for (i=0;i<10000;i++){
		sig = ECDSA_do_sign("alexandre morel", 15, eckey);
		printf("\n");
		display_sig(sig);
	}
	end = clock();
	cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
	printf("Time: %f\n", cpu_time_used);

	return 0;
}
